package com.sharelords.demo.web.service.impl;

import com.sharelords.demo.client.DemoTestClient;
import com.sharelords.demo.client.ao.DemoTestAo;
import com.sharelords.demo.client.param.ConditionParamAo;
import com.sharelords.demo.common.enums.ReturnCodeEnum;
import com.sharelords.demo.common.result.ServiceResult;
import com.sharelords.demo.common.result.ViewResult;
import com.sharelords.demo.common.result.helper.ViewResultHelper;
import com.sharelords.demo.common.util.InvokeUtil;
import com.sharelords.demo.web.request.ConditionReqParam;
import com.sharelords.demo.web.request.DemoTestSaveParam;
import com.sharelords.demo.web.request.DemoTestUpdateParam;
import com.sharelords.demo.web.service.DemoTestService;
import com.sharelords.demo.web.vo.DemoTestVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DemoTestServiceImpl implements DemoTestService {

    private static final Logger logger = LoggerFactory.getLogger(DemoTestServiceImpl.class);

    @Autowired
    private DemoTestClient demoTestClient;

    @Override
    public ViewResult<Integer> save(DemoTestSaveParam param) {
        if (param == null) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            ServiceResult<Integer> serviceResult = demoTestClient.saveSelective(InvokeUtil.copy(param, DemoTestAo.class));
            if (!ReturnCodeEnum.SUCCESS.getReturnCode().equals(serviceResult.getReturnCode())) {
                return ViewResultHelper.fail(serviceResult.getReturnCode(), serviceResult.getReturnMsg());
            }

            return ViewResultHelper.success(serviceResult.getResult());
        } catch (Exception e) {
            logger.error("新增发生异常，param={}，异常信息为：", param, e);
            return ViewResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ViewResult<Integer> update(DemoTestUpdateParam param) {
        if (param == null) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            // 参数组装
            DemoTestAo aoParam = InvokeUtil.copy(param, DemoTestAo.class);
            aoParam.setUpdateTime(new Date());
            aoParam.setUpdateBy("123456");

            // 更新
            ServiceResult<Integer> serviceResult = demoTestClient.updateSelective(aoParam);
            if (!ReturnCodeEnum.SUCCESS.getReturnCode().equals(serviceResult.getReturnCode())) {
                return ViewResultHelper.fail(serviceResult.getReturnCode(), serviceResult.getReturnMsg());
            }

            return ViewResultHelper.success(serviceResult.getResult());
        } catch (Exception e) {
            logger.error("更新发生异常，param={}，异常信息为：{}", param, e);
            return ViewResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ViewResult<Integer> delete(Long id) {
        if (id == null) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            ServiceResult<Integer> serviceResult = demoTestClient.deletePhysically(id);
            if (!ReturnCodeEnum.SUCCESS.getReturnCode().equals(serviceResult.getReturnCode())) {
                return ViewResultHelper.fail(serviceResult.getReturnCode(), serviceResult.getReturnMsg());
            }

            return ViewResultHelper.success(serviceResult.getResult());
        } catch (Exception e) {
            logger.error("删除发生异常，id={}，异常信息为：{}", id, e);
            return ViewResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ViewResult<DemoTestVo> getDetail(Long id) {
        if (id == null) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            ServiceResult<DemoTestAo> serviceResult = demoTestClient.queryByPrimaryKey(id);
            if (!ReturnCodeEnum.SUCCESS.getReturnCode().equals(serviceResult.getReturnCode())) {
                return ViewResultHelper.fail(serviceResult.getReturnCode(), serviceResult.getReturnMsg());
            }

            return ViewResultHelper.success(InvokeUtil.copy(serviceResult.getResult(), DemoTestVo.class));
        } catch (Exception e) {
            logger.error("根据id获取详情发生异常，id={}，异常信息为：{}", id, e);
            return ViewResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ViewResult<DemoTestVo> getDetailByName(String name) {
        if (StringUtils.isBlank(name)) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            ServiceResult<DemoTestAo> serviceResult = demoTestClient.queryByName(name);
            if (!ReturnCodeEnum.SUCCESS.getReturnCode().equals(serviceResult.getReturnCode())) {
                return ViewResultHelper.fail(serviceResult.getReturnCode(), serviceResult.getReturnMsg());
            }

            return ViewResultHelper.success(InvokeUtil.copy(serviceResult.getResult(), DemoTestVo.class));
        } catch (Exception e) {
            logger.error("根据姓名获取详情发生异常，name={}，异常信息为：{}", name, e);
            return ViewResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ViewResult<List<DemoTestVo>> getPageListByConditions(ConditionReqParam reqParam, Integer pageNum, Integer pageSize) {
        if (reqParam == null) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }
        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageNum < 0 || pageSize == null || pageSize <= 0) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_ERROR);
        }

        try {
            // 参数组装
            ConditionParamAo paramAo = InvokeUtil.copy(reqParam, ConditionParamAo.class);
            // 查询
            ServiceResult<List<DemoTestAo>> serviceResult = demoTestClient.queryPageByConditions(paramAo, pageNum, pageSize);
            if (!ReturnCodeEnum.SUCCESS.getReturnCode().equals(serviceResult.getReturnCode())) {
                return ViewResultHelper.fail(serviceResult.getReturnCode(), serviceResult.getReturnMsg());
            }

            return ViewResultHelper.success(InvokeUtil.copyList(serviceResult.getResult(), DemoTestVo.class));
        } catch (Exception e) {
            logger.error("多条件分页获取数据列表发生异常，reqParam={}，pageNum={}，pageSize={}，异常信息为：{}", reqParam, pageNum, pageSize, e);
            return ViewResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }

    @Override
    public ViewResult<Integer> getCount(ConditionReqParam reqParam) {
        if (reqParam == null) {
            return ViewResultHelper.fail(ReturnCodeEnum.PARAM_BLANK);
        }

        try {
            // 参数组装
            ConditionParamAo paramAo = InvokeUtil.copy(reqParam, ConditionParamAo.class);
            // 查询
            ServiceResult<Integer> serviceResult = demoTestClient.queryCountByConditions(paramAo);
            if (!ReturnCodeEnum.SUCCESS.getReturnCode().equals(serviceResult.getReturnCode())) {
                return ViewResultHelper.fail(serviceResult.getReturnCode(), serviceResult.getReturnMsg());
            }

            return ViewResultHelper.success(serviceResult.getResult());
        } catch (Exception e) {
            logger.error("多条件获取记录数发生异常，reqParam={}，异常信息为：{}", reqParam, e);
            return ViewResultHelper.fail(ReturnCodeEnum.ERROR);
        }
    }
}
